﻿using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Mvc;

public static class ActionContextExtension
{
    public static void SetContentDispositionHeader(this ActionContext context, IBpctFileStreamResult result)
    {
        context.HttpContext.Response.Headers[HeaderNames.AccessControlExposeHeaders] = HeaderNames.ContentDisposition;

        if (string.IsNullOrEmpty(result.FileDownloadName))
        {
            var contentDisposition = new ContentDispositionHeaderValue("inline");

            if (!string.IsNullOrWhiteSpace(result.FileInlineName))
            {
                contentDisposition.SetHttpFileName(result.FileInlineName);
            }

            context.HttpContext.Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();
        }
    }
}